﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class OndutyDataController : Base
{
    private readonly OndutyDataService _ondutyDataService;
    private readonly OndutyCatService _ondutyCatService;
    public OndutyDataController(OndutyDataService ondutyDataService, OndutyCatService ondutyCatService)
    {
        _ondutyDataService = ondutyDataService;
        _ondutyCatService = ondutyCatService;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery] QueryOndutyData query)
    {
        PagedInfo<ViewOndutyData> pagedList = await _ondutyDataService.GetPageListAsync(query);
        return Success(new { count = pagedList.TotalNum, data = pagedList.Result });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        IEnumerable<OndutyCat> catList = await _ondutyCatService.GetListAsync();
        OndutyData data = await _ondutyDataService.GetIdAsync(id);
        return Success(new { data, catList });
    }

    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_AddOrEdit)]
    [Log(Name = "新增值班")]
    public async Task<IActionResult> CreatePostAsync([FromBody] OndutyData dictData)
    {
        if (await _ondutyDataService.InsertDelCache(dictData) != null)
            return Success("新增值班");
        else
            return Fail("新增失败！");
    }


    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_AddOrEdit)]
    [Log(Name = "修改值班")]
    public async Task<IActionResult> EditPostAsync([FromBody] OndutyData dictData)
    {
        if (await _ondutyDataService.UpdateDelCache(dictData) > 0)
            return Success("成功修改值班 ID：" + dictData.Id);
        else
            return Fail("失败");
    }

    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.Dict_Delete)]
    [Log(Name = "删除值班")]
    public async Task<IActionResult> Delete(int id)
    {
        if (await _ondutyDataService.DeleteDelCache(id) > 0)
            return Success("删除值班 ID：" + id);
        else
            return Fail("删除失败");
    }
}
